<template>
  <div class="model">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="模型标识" prop="modelKey">
        <el-input
          v-model="queryParams.modelKey"
          placeholder="请输入模型标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程分类" prop="category">
        <el-select
          v-model="queryParams.category"
          clearable
          placeholder="请选择"
          size="small"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-upload"-->
      <!--          size="small"-->
      <!--          @click="handleImport"-->
      <!--          v-hasPermi="['workflow:model:import']"-->
      <!--        >导入</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['workflow:model:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workflow:model:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          size="small"
          @click="handleExport"
          v-hasPermi="['workflow:model:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      fit
      :data="modelList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="模型标识"
        align="center"
        prop="modelKey"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="模型名称"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleProcessView(scope.row)">
            <span>{{ scope.row.modelName }}</span>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="流程分类"
        align="center"
        prop="categoryName"
        :formatter="categoryFormat"
      />
      <el-table-column label="模型版本" align="center">
        <template #default="scope">
          <el-tag size="default">v{{ scope.row.version }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="描述"
        align="center"
        prop="description"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            size="small"
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workflow:model:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            size="small"
            icon="Scissor"
            @click="handleDesigner(scope.row)"
            v-hasPermi="['workflow:model:designer']"
            >设计</el-button
          >
          <el-button
            link
            type="primary"
            size="small"
            icon="Edit"
            v-hasPermi="['workflow:model:deploy']"
            @click.native="handleDeploy(scope.row)"
            >部署</el-button
          >
          <br />
          <el-button
            link
            type="primary"
            size="small"
            icon="Share"
            v-hasPermi="['workflow:model:deploy']"
            @click.native="handleProcessView(scope.row)"
            >图示</el-button
          >
          <el-button
            link
            type="primary"
            size="small"
            icon="Edit"
            v-hasPermi="['workflow:model:list']"
            @click.native="handleHistory(scope.row)"
            >历史</el-button
          >
          <el-button
            link
            type="primary"
            size="small"
            icon="DeleteFilled"
            v-hasPermi="['workflow:model:remove']"
            @click.native="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--  添加或修改模型信息对话框  -->
    <el-dialog
      :title="title"
      v-model="open"
      width="30%"
      append-to-body
      @close="cancel()"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模型标识" prop="modelKey">
          <el-input v-model="form.modelKey" clearable disabled />
        </el-form-item>
        <el-form-item label="模型名称" prop="modelName">
          <el-input
            v-model="form.modelName"
            clearable
            :disabled="form.modelId !== undefined"
          />
        </el-form-item>
        <el-form-item label="流程分类" prop="category">
          <el-select
            v-model="form.category"
            placeholder="请选择"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in categoryOptions"
              :key="item.categoryId"
              :label="item.categoryName"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel()">取 消</el-button>
      </div>
    </el-dialog>

    <!--  &lt;!&ndash; bpmn20.xml导入对话框 &ndash;&gt;-->
    <!--  <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body @close="cancel('uploadForm')">-->
    <!--    <el-upload-->
    <!--      ref="upload"-->
    <!--      :limit="1"-->
    <!--      accept=".xml"-->
    <!--      :headers="upload.headers"-->
    <!--      :action="upload.url + '?name=' + upload.name+'&category='+ upload.category"-->
    <!--      :disabled="upload.isUploading"-->
    <!--      :on-progress="handleFileUploadProgress"-->
    <!--      :on-success="handleFileSuccess"-->
    <!--      :auto-upload="false"-->
    <!--      drag-->
    <!--    >-->
    <!--      <i class="el-icon-upload"></i>-->
    <!--      <div class="el-upload__text">-->
    <!--        将文件拖到此处，或-->
    <!--        <em>点击上传</em>-->
    <!--      </div>-->
    <!--      <div class="el-upload__tip" slot="tip">-->
    <!--        <el-form ref="uploadForm" :model="upload" size="small" :rules="rules" label-width="80px">-->
    <!--          <el-form-item label="流程名称" prop="name">-->
    <!--            <el-input v-model="upload.name" clearable/>-->
    <!--          </el-form-item>-->
    <!--          <el-form-item label="流程分类" prop="category">-->
    <!--            <el-select v-model="upload.category" placeholder="请选择" clearable style="width:100%">-->
    <!--              <el-option v-for="item in categoryOptions" :key="item.categoryId" :label="item.categoryName"-->
    <!--                         :value="item.code"/>-->
    <!--            </el-select>-->
    <!--          </el-form-item>-->
    <!--        </el-form>-->
    <!--      </div>-->
    <!--      <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“bpmn20.xml”格式文件！</div>-->
    <!--    </el-upload>-->
    <!--    <div slot="footer" class="dialog-footer">-->
    <!--      <el-button type="primary" @click="submitFileForm">确 定</el-button>-->
    <!--      <el-button @click="cancel('uploadForm')">取 消</el-button>-->
    <!--    </div>-->
    <!--  </el-dialog>-->

    <!-- 流程图预览 -->
    <el-dialog
      :title="processView.title"
      v-model="processView.open"
      class="process-previewer"
      fullscreen
      append-to-body
    >
      <process-previewer
        :key="`designer-${processView.index}`"
        :xml="processView.xmlData"
      />
    </el-dialog>

    <!-- 流程模型历史 -->
    <el-dialog
      title="模型历史"
      fullscreen
      v-model="history.open"
      class="plan-dialog"
    >
      <el-table
        v-loading="history.loading"
        fit
        :data="historyList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="模型标识"
          align="center"
          prop="modelKey"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="模型名称"
          align="center"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <el-button
              type="primary"
              link
              @click="handleProcessView(scope.row)"
            >
              <span>{{ scope.row.modelName }}</span>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="流程分类"
          align="center"
          prop="categoryName"
          :formatter="categoryFormat"
        />
        <el-table-column label="模型版本" align="center">
          <template #default="scope">
            <el-tag size="default">v{{ scope.row.version }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="描述"
          align="center"
          prop="description"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              icon="el-icon-video-play"
              v-hasPermi="['workflow:model:deploy']"
              @click.native="handleDeploy(scope.row)"
              >部署</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              icon="el-icon-star-off"
              v-hasPermi="['workflow:model:save']"
              @click.native="handleLatest(scope.row)"
              >设为最新</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="historyTotal > 0"
        :total="historyTotal"
        :page.sync="queryHistoryParams.pageNum"
        :limit.sync="queryHistoryParams.pageSize"
        @pagination="getHistoryList"
      />
    </el-dialog>

    <!-- 设计流程图【已转移至单页面】 -->
    <!--
    <el-dialog
      :title="designerData.title"
      v-model="designerOpen"
      append-to-body
      fullscreen
    >
      <process-designer
        :key="designerOpen"
        style="border: 1px solid rgba(0, 0, 0, 0.1)"
        ref="modelDesigner"
        v-loading="designerData.loading"
        :bpmnXml="designerData.bpmnXml"
        :designerForm="designerData.form"
        @save="onSaveDesigner"
      />
    </el-dialog>
  --></div>
</template>

<script setup name="Model">
/**
 * 此界面用以【增、删、改、设计】流程模型
 */
const { proxy } = getCurrentInstance();
import { getToken } from "@/utils/auth";
import {
  getBpmnXml,
  listModel,
  historyModel,
  latestModel,
  addModel,
  updateModel,
  saveModel,
  delModel,
  deployModel,
} from "@/api/workflow/model";
import { listCategory } from "@/api/workflow/category";

const data = reactive({
  // 页面基本数据
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 流程模型表格数据
  modelList: [],
  // 流程分类选项
  categoryOptions: [],
  title: "",
  open: false,
  form: {},
  // 表单校验
  rules: {
    modelKey: [
      { required: true, message: "模型标识不能为空", trigger: "blur" },
    ],
    modelName: [
      { required: true, message: "模型名称不能为空", trigger: "blur" },
    ],
    category: [{ required: true, message: "请选择类型", trigger: "change" }],
  },

  // 流程设计模块
  designerOpen: false,
  designerData: {
    loading: false,
    bpmnXml: "",
    modelId: null,
    form: {
      processName: null,
      processKey: null,
    },
  },
  designerModelId: null,
  processView: {
    title: "",
    open: false,
    index: undefined,
    xmlData: "",
  },
  // bpmn.xml 导入
  upload: {
    // 是否显示弹出层（xml导入）
    open: false,
    // 弹出层标题（xml导入）
    title: "",
    // 是否禁用上传
    isUploading: false,
    name: null,
    category: null,
    // 设置上传的请求头部
    headers: { Authorization: "Bearer " + getToken() },
    // 上传的地址
    url: import.meta.env.VITE_APP_BASE_API + "/workflow/definition/import",
  },
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    modelKey: null,
    modelName: null,
    category: null,
  },
  currentRow: null,
  // 历史数据
  history: {
    open: false,
    loading: false,
  },
  historyList: [],
  historyTotal: 0,
  queryHistoryParams: {
    pageNum: 1,
    pageSize: 10,
    modelKey: null,
  },
});

const {
  // 页面基本数据
  loading,
  ids,
  single,
  multiple,
  showSearch,
  total,
  modelList,

  categoryOptions,
  title,
  open,
  form,
  queryParams,
  rules,

  // 流程设计模块
  designerOpen,
  designerData,
  designerModelId,
  processView,
  upload,
  currentRow,
  // 历史数据
  history,
  historyList,
  historyTotal,
  queryHistoryParams,
} = toRefs(data);

/** 查询流程分类列表 */
function getCategoryList() {
  listCategory().then((response) => (categoryOptions.value = response.rows));
}
/** 查询流程模型列表 */
function getList() {
  loading.value = true;
  listModel(queryParams.value).then((response) => {
    modelList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function cancel() {
  reset();
  // 关闭dialog
  open.value = false;
}
// 表单重置
function reset() {
  form.value = {
    modelId: undefined,
    modelKey: undefined,
    modelName: undefined,
    category: undefined,
    description: undefined,
  };
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.$refs.queryForm.resetFields();
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.modelId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 部署流程 */
function handleDeploy(row) {
  loading.value = true;
  deployModel({
    modelId: row.modelId,
  })
    .then((response) => {
      proxy.$modal.msgSuccess(response.msg);
      let obj = { name: "Deploy", path: "/workflow/deploy" };
      returnproxy.$store.dispatch("tagsView/delCachedView", obj).then(() => {
        proxy.$router.push(obj);
      });
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 查看流程图 */
function handleProcessView(row) {
  let modelId = row.modelId;
  processView.value.title = "流程图";
  processView.value.index = modelId;
  // 发送请求，获取xml
  getBpmnXml(modelId).then((response) => {
    processView.value.xmlData = response.data;
  });
  processView.value.open = true;
}
function getHistoryList() {
  history.value.loading = true;
  historyModel(queryHistoryParams.value).then((response) => {
    historyTotal.value = response.total;
    historyList.value = response.rows;
    history.value.loading = false;
  });
}
function handleHistory(row) {
  history.value.open = true;
  queryHistoryParams.value.modelKey = row.modelKey;
  getHistoryList();
}
/** 设为最新版 */
function handleLatest(row) {
  proxy.$modal.confirm("是否确认将此版本设为最新？").then(() => {
    history.value.loading = true;
    latestModel({
      modelId: row.modelId,
    })
      .then((response) => {
        history.value.open = false;
        getList();
        proxy.$modal.msgSuccess(response.msg);
      })
      .finally(() => {
        history.value.loading = false;
      });
  });
}
function handleCurrentChange(data) {
  if (data) {
    currentRow.value = JSON.parse(data.content);
  }
}
function handleAdd() {
  title.value = "新增流程模型";
  const dateTime = new Date().getTime();
  form.value = {
    modelKey: `Process_${dateTime}`,
    modelName: `业务流程_${dateTime}`,
  };
  open.value = true;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  title.value = "修改流程模型";
  form.value = {
    modelId: row.modelId,
    modelKey: row.modelKey,
    modelName: row.modelName,
    category: row.category,
    description: row.description,
  };
  open.value = true;
}
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.modelId !== undefined) {
        updateModel(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addModel(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 设计按钮操作 */
function handleDesigner(row) {
  /*
  designerData.value.title = "流程设计 - " + row.modelName;
  designerData.value.modelId = row.modelId;
  designerData.value.form = {
    processName: row.modelName,
    processKey: row.modelKey,
  };
  if (row.modelId) {
    designerData.value.loading = true;
    getBpmnXml(row.modelId).then((response) => {
      designerData.value.bpmnXml = response.data || "";
      designerData.value.loading = false;
      designerOpen.value = true;
    });
  }
  */
  proxy.$router.push({
    path: "/tool/build/process",
    query: { modelId: row.modelId },
  });

  proxy.$tab.closeOpenPage({
    path: "/tool/build/process",
    query: { modelId: row.modelId },
  });
}
function onSaveDesigner(bpmnXml) {
  bpmnXml = bpmnXml;
  let dataBody = {
    modelId: designerData.modelId,
    bpmnXml: bpmnXml,
  };
  proxy
    .confirm("是否将此模型保存为新版本？", "提示", {
      distinguishCancelAndClose: true,
      confirmButtonText: "是",
      cancelButtonText: "否",
    })
    .then(() => {
      confirmSave(dataBody, true);
    })
    .catch((action) => {
      if (action === "cancel") {
        confirmSave(dataBody, false);
      }
    });
}
function confirmSave(body, newVersion) {
  designerData.value.loading = true;
  saveModel(
    Object.assign(body, {
      newVersion: newVersion,
    })
  )
    .then(() => {
      designerOpen.value = false;
      getList();
    })
    .finally(() => {
      designerData.value.loading = false;
    });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const modelIds = row.modelId || ids;
  proxy.$modal
    .confirm('是否确认删除模型编号为"' + modelIds + '"的数据项？')
    .then(() => {
      loading.value = true;
      return delModel(modelIds);
    })
    .then(() => {
      loading.value = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .finally(() => {
      loading.value = false;
    });
}
/** 导出按钮操作 */
function handleExport() {
  download(
    "workflow/model/export",
    {
      ...queryParams.value,
    },
    `wf_model_${new Date().getTime()}.xlsx`
  );
}
/** 导入bpmn.xml文件 */
function handleImport() {
  upload.value.title = "bpmn20.xml文件导入";
  upload.value.open = true;
}
// 文件上传中处理
function handleFileUploadProgress(event, file, fileList) {
  upload.value.isUploading = true;
}
// 文件上传成功处理
function handleFileSuccess(response, file, fileList) {
  upload.value.open = false;
  upload.value.isUploading = false;
  proxy.$refs.upload.clearFiles();
  proxy.$message.success(response.msg);
  getList();
}
// 提交上传文件
function submitFileForm() {
  proxy.$refs.uploadForm.validate((valid) => {
    if (valid) {
      proxy.$refs.upload.submit();
    }
  });
}
function categoryFormat(row, column) {
  return (
    categoryOptions.value.find((k) => k.code === row.category)?.categoryName ??
    ""
  );
}
function submitSave() {
  getList();
}

getCategoryList();
getList();
</script>

<style lang="less" scoped>
.model {
  padding: 10px;
  height: 100%;
  :deep(.diagram-note) {
    background-color: rgba(66, 180, 21, 0.7);
    color: White;
    border-radius: 5px;
    font-family: Arial;
    font-size: 12px;
    padding: 5px;
    min-height: 16px;
    width: 50px;
    text-align: center;
  }
  :deep(.highlight-overlay) {
    background-color: green; /* color elements as green */
    opacity: 0.4;
    pointer-events: none; /* no pointer events, allows clicking through onto the element */
  }
}

.content,
.content > div {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.content > .message {
  text-align: center;
  display: table;

  font-size: 16px;
  color: #111;
}

.content > .message .note {
  vertical-align: middle;
  text-align: center;
  display: table-cell;
}

.content .error .details {
  max-width: 500px;
  font-size: 12px;
  margin: 20px auto;
  text-align: left;
}

.content .error pre {
  border: solid 1px #ccc;
  background: #eee;
  padding: 10px;
}

.content:not(.with-error) .error,
.content.with-error .intro,
.content.with-diagram .intro {
  display: none;
}

.content .canvas,
.content.with-error .canvas {
  visibility: hidden;
}

.content.with-diagram .canvas {
  visibility: visible;
}

.buttons {
  position: fixed;
  bottom: 20px;
  right: 20px;

  padding: 0;
  margin: 0;
  list-style: none;
}

.buttons > li {
  display: inline-block;
  margin-right: 10px;
}
.buttons > li > a {
  background: #ddd;
  border: solid 1px #666;
  display: inline-block;
  padding: 5px;
}

.buttons a {
  opacity: 0.3;
}

.buttons a.active {
  opacity: 1;
}
</style>
